<!-- 用户管理 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true" label-width="72px">
      <el-form-item label="用户名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入用户名" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="用户类型" prop="type">
        <el-select clearable v-model="queryParams.type" placeholder="请选择">
          <el-option v-for="dict in userTypeList" :key="dict.value" :value="dict.value" :label="dict.label" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="userList" border>
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="用户名称" prop="user_name" />
      <el-table-column label="用户昵称" prop="nick_name" :show-overflow-tooltip="true" />
      <el-table-column label="状态" prop="status" width="100">
        <template #default="scope">
          {{ statusObj[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column label="用户类型" prop="type" width="150">
        <template #default="scope">
          {{ typeObj[scope.row.type] }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" @click="handleDelete(scope.row)">删除</el-button>
          <el-button link type="primary" @click="handleResetPasswd(scope.row)">重置密码</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.page_num"
      v-model:limit="queryParams.page_size"
      @pagination="getList"
    />

    <!-- 新增和修改用户信息的弹框 -->
    <template v-if="visible">
      <UserDialog v-model="visible" :rowInfo="rowInfo" @success="success" />
    </template>
  </div>
</template>

<script setup name="User">
import UserDialog from "./userDialog.vue";
import { getOptionsList } from "@/api/common";
import { getUserList, removeUser, resetPasswd } from "@/api/system/user";
const { proxy } = getCurrentInstance();
const showSearch = ref(true);
const loading = ref(false);
const queryParams = ref({
  page_num: 1,
  page_size: 10,
  name: "",
  type: ""
});
const userList = ref([]);
const total = ref(0);
const visible = ref(false);
const rowInfo = ref({});
const statusList = ref([]);
const userTypeList = ref([]);

const statusObj = reactive({});
const typeObj = reactive({});

getStatusList();
getTypeList();
getList();

async function getStatusList() {
  const res = await getOptionsList({ dict_type: "COMMON_STATUS" });
  if (res.code === 0) {
    statusList.value = res.data.map((item) => {
      statusObj[item.value] = item.label;
      return item;
    });
  }
}

async function getTypeList() {
  const res = await getOptionsList({ dict_type: "USER_TYPE" });
  if (res.code === 0) {
    userTypeList.value = res.data.map((item) => {
      typeObj[item.value] = item.label;
      return item;
    });
  }
}

/** 查询列表 */
function getList() {
  loading.value = true;
  getUserList({ ...queryParams.value })
    .then((res) => {
      const data = res.data;
      userList.value = data.list;
      total.value = data.total;
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page_num = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 添加修改成功回调
function success(type) {
  if (type === "add") {
    handleQuery();
  } else {
    resetQuery();
  }
}
/** 添加 */
function handleAdd() {
  rowInfo.value = {};
  visible.value = true;
}
/** 修改 */
function handleUpdate(row) {
  rowInfo.value = row;
  visible.value = true;
}
/** 删除按钮操作 */
function handleDelete(row) {
  const name = row.user_name;
  proxy.$modal
    .confirm('是否确认删除用户名称为"' + name + '"的数据项?')
    .then(function () {
      return removeUser({ id: row.id });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

// 重置密码
function handleResetPasswd(row) {
  const name = row.user_name;
  proxy.$modal
    .confirm('是否确认重置用户名称为"' + name + '"的数据项?')
    .then(function () {
      return resetPasswd({ id: row.id });
    })
    .then(() => {
      proxy.$modal.msgSuccess("密码重置成功");
    })
    .catch(() => {});
}
</script>
